<?php
/**
 * beimuaihui System
 * Copyright(c) 2011-2020 beimuaihui.
 * @license    http://www.gnu.org/licenses/gpl.html     This software Under GPL V3 License 
 * beimuaihui@gmail.com
 * http://code.google.com/p/beimuaihui/
 * $Id: index.php 213 2011-03-09 07:40:02Z beimuaihui $
 */
//echo '<pre>';print_r($_POST);print_r($_FILES);exit;
error_reporting(E_ALL | E_STRICT);
ini_set('display_errors', 1);
date_default_timezone_set('Asia/Hong_Kong');

session_name(md5($_SERVER['HTTP_HOST']));
session_start();

define("BAOGG_ROOT",realpath(dirname(dirname(__FILE__) ))."/"); //should change with  different parent directory

$aIncludePath = array(BAOGG_ROOT);
$aIncludePath[] = BAOGG_ROOT. 'library/';
$aIncludePath[] = BAOGG_ROOT . 'models/';
set_include_path(implode(PATH_SEPARATOR, $aIncludePath));


require_once 'Zend/Loader/Autoloader.php'; ////Zend_Loader::registerAutoload();
Zend_Loader::loadFile('smarty/Smarty.class.php');
$autoloader = Zend_Loader_Autoloader::getInstance();
$autoloader->pushAutoloader('Smarty', 'smartyAutoload');
$autoloader->setFallbackAutoloader(true);


Zend_Loader::loadFile('config/config.php',null,true);
Zend_Loader::loadClass("Zend_Registry");
Zend_Loader::loadClass("Baogg_Db");
Zend_Loader::loadClass("Baogg_Controller");
Zend_Loader::loadClass("Baogg_Controller_Plugin");
Zend_Loader::loadClass('Baogg_View_Smarty');
Zend_Loader::loadClass("Zend_Db_Profiler_Firebug");
//Zend_Loader::loadClass("Zend_Log");
//Zend_Loader::loadClass("Zend_Log_Writer_Firebug");

$db=new Zend_Db_Adapter_Mysqli(array(
    'host'     => 'localhost',
    'username' => 'root',
    'password' => '',
    'dbname'   => 'baogg',
	'driver_options' => array(
			MYSQLI_INIT_COMMAND => 'set names utf8'
		)
));

/*
$db= new Zend_Db_Adapter_Pdo_Mysql(array(
    'host'     => 'localhost',
    'username' => 'root',
    'password' => '',
    'dbname'   => 'baogg',
	'driver_options' => array(
			PDO::MYSQL_ATTR_INIT_COMMAND => 'set names utf8'
		)
));
*/

//just for debug
$profiler = new Zend_Db_Profiler_Firebug('All DB Queries');
$profiler->setEnabled(true);
$db->setProfiler($profiler); 


/*$writer = new Zend_Log_Writer_Firebug();
$logger = new Zend_Log($writer);
//global $logger;$logger->log('This is a log message!', Zend_Log::INFO);
*/
Zend_Registry::set('db', $db);


//load default language
Zend_Loader::loadClass("Language");
$lang=new Language();
$rs_language=$lang->getList(array("is_default"=>1));
if($rs_language &&  $rs_language[0]){
	define ( "BAOGG_LANGUAGE", $rs_language[0]['language_code'] );
	define ( "BAOGG_CHARSET", $rs_language[0]['charset'] );
	define ( "BAOGG_LANGUAGE_ID", $rs_language[0]['language_id'] );
	header('Content-Type: text/html; charset=utf-8');
}else{
	throw new Exception("Not select default DB");
	exit;
}
 
//loaded in Baogg_Controller_Plugin class
$LANG=array(); 

/**
 * Setup controller
 */
//$_SESSION['admin_user_id'] = 2;//unset($_SESSION);
$controller = Zend_Controller_Front::getInstance();
$aControllerDirectory['system'] = BAOGG_ROOT . 'admin/System/';
//$aControllerDirectory['blog'] = realpath(BAOGG_UPLOAD_DIR . '../admin/Blog/controllers') . "/";
$aControllerDirectory['mall'] = BAOGG_ROOT . 'admin/Mall/';
$aControllerDirectory['multilang'] = BAOGG_ROOT . 'admin/Multilang/';
$aControllerDirectory['dict'] = BAOGG_ROOT . 'admin/Dict/';
$controller->setControllerDirectory($aControllerDirectory);

$controller->registerPlugin(new Baogg_Controller_Plugin());
$controller->throwExceptions(true); // should be turned on in development time



//set view 
$aSmartyConfig = array(
					'debugging' => 1,  //0
					'left_delimiter' => "{", 
					'right_delimiter' => "}", 
					'compile_check' => 1, 
					'force_compile' => 1, 
					'caching' => 0,   //1
					'cache_lifetime' => 0,  //20
					'SMARTY_DIR' => BAOGG_ROOT. 'library/smarty/', 
					//'SMARTY_CORE_DIR' => BAOGG_ROOT. 'library/smarty/internals/', 
					'template_dir' => BAOGG_ROOT. 'views/admin', 
					'compile_dir' => BAOGG_ROOT . 'views/admin_c', 
					'cache_dir' =>BAOGG_ROOT. '/views/admin_cache', 
					'config_dir' =>BAOGG_ROOT. '/views/admin_configs', 
					'plugins_dir' => array(
												BAOGG_ROOT. 'library/smarty/plugins'
												//      'cache_handler_func'   => smarty_cache_eaccelerator, 
												//BAOGG_ROOT. '/views/admin_plugins'
												));
/*
//var_dump(realpath(BAOGG_UPLOAD_DIR . '../') . '/html/templates');exit;
$smarty = new Baogg_View_Smarty(null, $aSmartyConfig);
//$smarty->addScriptPath($viewConf['scriptPath']);
Zend_Registry::set('smarty', $smarty);
*/
//var_dump($aSmartyConfig['template_dir']);exit;												
$view = new Baogg_View_Smarty(null,$aSmartyConfig);

$viewRenderer =  Zend_Controller_Action_HelperBroker::getStaticHelper('ViewRenderer');
$viewRenderer->setView($view)
             ->setViewBasePathSpec($aSmartyConfig['template_dir'])
             ->setViewScriptPathSpec(':module/:controller_:action.:suffix')
             ->setViewScriptPathNoControllerSpec(':action.:suffix')
             ->setViewSuffix('tpl');
 Zend_Registry::set('smarty', $view);
             

$controller->setBaseUrl(BAOGG_BASE_URL);


$controller->setParam('prefixDefaultModule', true);
$controller->setDefaultModule('mall');
$controller->setDefaultControllerName('admin');
$controller->setDefaultAction('home');

// run!
$controller->dispatch();
